﻿/************************************************************************

   Extended Silverlight Toolkit

   Copyright (C) 2010-2012 Xceed Software Inc.

   This program is provided to you under the terms of the Microsoft Public
   License (Ms-PL) as published at http://extendedsilverlight.codeplex.com/license

   Please purchase a commercial version of this toolkit if you are using
   it in a commercial product. The commercial versions support the project,
   and also include more features for each control, priority support,
   source code, updates, removed watermark, and a proprietary license.

   Visit http://xceed.com and follow @datagrid on Twitter.

  **********************************************************************/

using System.Windows;
using System.Windows.Media.Animation;

namespace Xceed.Silverlight.ListBox
{
  internal class PositionAnimation : DependencyObject, IAnimation
  {
    #region XChange Property

    internal static readonly DependencyProperty XChangeProperty = DependencyProperty.Register(
      "XChange",
      typeof( double ),
      typeof( PositionAnimation ),
      new PropertyMetadata( ( double )0.0d ) );

    internal double XChange
    {
      get
      {
        return ( double )this.GetValue( PositionAnimation.XChangeProperty );
      }
      set
      {
        this.SetValue( PositionAnimation.XChangeProperty, value );
      }
    }

    #endregion

    #region YChange Property

    internal static readonly DependencyProperty YChangeProperty = DependencyProperty.Register(
      "YChange",
      typeof( double ),
      typeof( PositionAnimation ),
      new PropertyMetadata( ( double )0.0d ) );

    internal double YChange
    {
      get
      {
        return ( double )this.GetValue( PositionAnimation.YChangeProperty );
      }
      set
      {
        this.SetValue( PositionAnimation.YChangeProperty, value );
      }
    }

    #endregion

    #region ActualXChange Property

    internal static readonly DependencyProperty ActualXChangeProperty = DependencyProperty.Register(
      "ActualXChange",
      typeof( double ),
      typeof( PositionAnimation ),
      new PropertyMetadata( ( double )0.0d ) );

    internal double ActualXChange
    {
      get
      {
        return ( double )this.GetValue( PositionAnimation.ActualXChangeProperty );
      }
      set
      {
        this.SetValue( PositionAnimation.ActualXChangeProperty, value );
      }
    }

    #endregion

    #region ActualYChange Property

    internal static readonly DependencyProperty ActualYChangeProperty = DependencyProperty.Register(
      "ActualYChange",
      typeof( double ),
      typeof( PositionAnimation ),
      new PropertyMetadata( ( double )0.0d ) );

    internal double ActualYChange
    {
      get
      {
        return ( double )this.GetValue( PositionAnimation.ActualYChangeProperty );
      }
      set
      {
        this.SetValue( PositionAnimation.ActualYChangeProperty, value );
      }
    }

    #endregion

    #region IAnimation Implementation

    AnimationDirection IAnimation.AnimationDirection
    {
      get
      {
        return m_animationDirection;
      }
      set
      {
        if( m_animationDirection != value )
        {
          m_animationDirection = value;
        }
      }
    }


    IEasingFunction IAnimation.EasingFunction
    {
      get
      {
        return m_easingFunction;
      }
      set
      {
        if( m_easingFunction != value )
        {
          m_easingFunction = value;
        }
      }
    }

    TransitionAnimation IAnimation.ParentTransition
    {
      get
      {
        return m_parentTransition;
      }
      set
      {
        if( m_parentTransition != value )
        {
          m_parentTransition = value;
        }
      }
    }

    void IAnimation.ComputeAnimationUpdate( double normalizedTime )
    {
      double easedTime = normalizedTime;
      if( m_easingFunction != null )
      {
        easedTime = m_easingFunction.Ease( normalizedTime );
      }

      if( m_animationDirection == AnimationDirection.ToReferencePosition )
      {
        if( !double.IsNaN( this.XChange ) )
        {
          this.ActualXChange = this.XChange - ( easedTime * this.XChange );
        }

        if( !double.IsNaN( this.YChange ) )
        {
          this.ActualYChange = this.YChange - ( easedTime * this.YChange );
        }
      }
      else
      {
        if( !double.IsNaN( this.XChange ) )
        {
          this.ActualXChange = ( easedTime * this.XChange );
        }

        if( !double.IsNaN( this.YChange ) )
        {
          this.ActualYChange = ( easedTime * this.YChange );
        }

      }
    }

    #endregion

    #region Private Members

    private TransitionAnimation m_parentTransition = null;
    private IEasingFunction m_easingFunction = null;
    private AnimationDirection m_animationDirection = AnimationDirection.ToReferencePosition;

    #endregion
  }
}
